﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace Honestysoft.Financing.Model
{
    public partial class Bu_AuditTable_FGW
    {
        public Bu_AuditTable_FGW()
        {
        }

        private Bu_BatchSummary _batchSummary;
        /// <summary>
        /// 本批次支付汇总表（深超）
        /// 说明：必须赋值
        /// </summary>
        public Bu_BatchSummary BatchSummary
        {
            get
            {
                return _batchSummary;
            }
            set
            {
                _batchSummary = value;
                BatchNo = _batchSummary.BatchNo;
                ReportDate = _batchSummary.PrintDate;
            }
        }
        /// <summary>
        /// 本批次本次支付汇总表（发改委）之前的支付汇总表（发改委）
        /// 说明：必须赋值，支付汇总表的 Counter 小于本次 Counter
        /// </summary>
        public List<Bu_AuditTable_FGW> PriorFGW
        {
            get;
            set;
        }
        /// <summary>
        /// 本批次本次支付汇总表（发改委）之前的支付汇总表（发改委）的作业表
        /// 说明：必须赋值，支付汇总表的 Counter 小于本次 Counter
        public DataTable PriorFGWDetail
        {
            get;
            set;
        }
        /// <summary>
        /// 显示的备注1
        /// </summary>
        public string DisRemark1
        {
            get { return "本表根据深超公司融资项目支付申请初审报告，经市发改委审核同意后汇总。"; }
        }

        /// <summary>
        /// 重新设定选择的作业表的相关数据
        /// </summary>
        public void ResetSelectedAT()
        {
            IsEnd = false;
            if (ThisTimeAT == null || ThisTimeAT.Rows.Count == 0)
            {
                ATCount = 0;
                Amount_DocRec_S = 0;
                Amount_S = 0;
                ProjectCount = 0;
                ContractCount = 0;
                OwnerCount = 0;
                ContractorCount = 0;
            }
            else
            {
                DataView dvTemp = new DataView(BatchSummaryDetail);
                string filter = "";
                foreach (DataRow dr in ThisTimeAT.Rows)
                {
                    if (filter != "") { filter += "','"; }
                    filter += dr["ATID"].ToString();
                }
                filter = "ID IN ('" + filter + "')";

                Amount_DocRec_S = Convert.ToDecimal(BatchSummaryDetail.Compute("SUM(Amount_DocRec_S)", filter));
                Amount_S = Convert.ToDecimal(BatchSummaryDetail.Compute("SUM(Amount_S)", filter));
                dvTemp.RowFilter = filter;
                ATCount = dvTemp.Count;
                //foreach (DataRowView drv in dvTemp)
                //{
                //    Amount_DocRec_S += Convert.ToDecimal(drv["Amount_DocRec_S"]);
                //    Amount_S += Convert.ToDecimal(drv["Amount_S"]);
                //}
                ProjectCount = dvTemp.ToTable(true, "ProjectName").Rows.Count;
                ContractCount = dvTemp.ToTable(true, "ContractID").Rows.Count;
                OwnerCount = dvTemp.ToTable(true, "OwnerName").Rows.Count;
                ContractorCount = dvTemp.ToTable(true, "ContractorName").Rows.Count;
                if (PriorFGWDetail != null)
                {
                    if (PriorFGWDetail.Rows.Count + ThisTimeAT.Rows.Count == BatchSummaryDetail.Rows.Count)
                    {
                        IsEnd = true;
                    }
                }
                else
                {
                    if (ThisTimeAT.Rows.Count == BatchSummaryDetail.Rows.Count)
                    {
                        IsEnd = true;
                    }
                }
            }
        }

        public string GetRemark2()
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("{0:yyyy年M月d日}", BatchSummary.PrintDate);
            sb.AppendFormat("深超公司共报送{0}个项目、{1}份合同，审核金额共计{2:N}元；", BatchSummary.ProjectCount, BatchSummary.ContractCount, BatchSummary.SumAmount_S);
            if (PriorFGW != null)
            {
                for (int i = 0; i < PriorFGW.Count; i++)
                {
                    sb.AppendFormat("{0:yyyy年M月d日}", PriorFGW[i].PrintDate);
                    sb.Append("经市发改委审核后同意支付");
                    sb.AppendFormat("{0}个项目、{1}份合同，金额{2:N}。", PriorFGW[i].ProjectCount, PriorFGW[i].ContractCount, PriorFGW[i].Amount_S);
                }
            }
            sb.AppendFormat("{0:yyyy年M月d日}", PrintDate);
            sb.Append("经市发改委审核后同意支付");
            if (Counter > 1 && IsEnd)
            {
                sb.Append("剩余的");
            }
            sb.AppendFormat("{0}个项目、{1}份合同，金额{2:N}。", ProjectCount, ContractCount, Amount_S);
            if (IsEnd)
            {
                sb.Append("（本批次支付完毕）");
            }
            return sb.ToString();
        }

        /// <summary>
        /// 显示的备注2
        /// </summary>
        public string DisRemark2
        {
            get
            {
                return GetRemark2();
            }
        }

        /// <summary>
        /// 本批次资金支付申请初审情况汇总表（深超提交给发改委的文件）
        /// 说明：必须赋值，来自GetBatchSummary存储过程
        /// </summary>
        public DataTable BatchSummaryDetail
        {
            get;
            set;
        }


        /// <summary>
        /// 本次选择的作业表，修改要必须调用SetCount()
        /// </summary>
        public DataTable ThisTimeAT
        {
            get;
            set;
        }

    }
}
